<template>
  <van-popup v-model="isShow" position="left" closeable @close="edit = false">
    <div class="article-channel">
      <div class="head">
        <h3>我的频道：<small>点击进入频道</small></h3>
        <a
          href="javascript:;"
          class="edit"
          :class="{ active: edit }"
          @click="edit = !edit"
        >
          {{ edit ? '完成' : '编辑' }}
        </a>
      </div>
      <div class="body">
        <a
          href="javascript:;"
          v-for="(item, index) in channelsList"
          :key="item.id"
          :class="{ active: activeIndex === item.id }"
          @click="enterChannel(item.id)"
        >
          {{ item.name }}
          <geek-icon
            v-show="edit && index !== 0 && activeIndex !== index"
            name="tag-close"
            @click.native.stop="delChannel(index)"
          ></geek-icon>
        </a>
      </div>
      <div class="head" style="margin-top:12px">
        <h3>频道推荐：</h3>
      </div>
      <div class="body">
        <a
          href="javascript:;"
          v-for="item in newChannels"
          :key="item.id"
          @click="addChannel(item)"
        >
          + {{ item.name }}
        </a>
      </div>
    </div>
  </van-popup>
</template>

<script>
import { channels, userChannelsPut } from '@/api/home.js'
export default {
  name: 'ArticleChannel',
  props: {
    channelsList: {
      type: Array,
      default: () => []
    },
    activeIndex: {
      type: Number,
      default: 0
    }
  },
  data () {
    return {
      isShow: false,
      allChannels: [],
      edit: false
    }
  },
  computed: {
    newChannels () {
      return this.allChannels.filter(item => {
        return !this.channelsList.find(i => {
          return i.id === item.id
        })
      })
    }
  },
  methods: {
    async getAllChannels () {
      const res = await channels()
      this.allChannels = res.data.channels
    },
    enterChannel (id) {
      this.$emit('update:activeIndex', id)
      this.isShow = false
    },
    async addChannel (item) {
      this.channelsList.push(item)
      this.channelsList.map((i, index) => {
        i.seq = index
        return i
      })
      await userChannelsPut(this.channelsList)
      this.$toast.success('新增成功')
    },
    async delChannel (index) {
      this.channelsList.splice(index, 1)
      this.channelsList.map((i, index) => {
        i.seq = index
        return i
      })
      await userChannelsPut(this.channelsList)
      this.$toast.success('删除成功')
    }
  },
  created () {
    this.getAllChannels()
  }
}
</script>

<style lang="less" scoped>
.van-popup {
  width: 100%;
  height: 100%;

  ::v-deep .van-popup__close-icon {
    font-size: 20px;
    right: 12px;
    top: 12px;
  }

  .article-channel {
    margin-top: 44px;

    .head {
      padding: 0 16px;
      display: flex;
      justify-content: space-between;
      justify-items: center;
      padding-bottom: 12px;

      h3 {
        font-size: 16px;
        color: #333;
        margin: 0;

        small {
          font-size: 12px;
          color: #999;
          margin-left: 10px;
        }
      }

      .edit {
        float: right;
        height: 22px;
        width: 52px;
        line-height: 22px;
        text-align: center;
        color: #de644b;
        border-radius: 11px;
        border: 1px solid #de644b;
        font-size: 12px;

        &.active {
          color: #fff;
          background: #de644b;
        }
      }
    }

    .body {
      padding: 0 6px 0 16px;

      a {
        display: inline-block;
        padding: 0 8px;
        font-size: 14px;
        color: #3a3948;
        background: #f7f8fa;
        height: 36px;
        line-height: 36px;
        min-width: 78px;
        margin-right: 10px;
        margin-bottom: 12px;
        border-radius: 18px;
        text-align: center;
        position: relative;

        &.active {
          color: @geek-color;
        }

        .geek-icon {
          position: absolute;
          top: -5px;
          right: -5px;
          line-height: 1;
        }
      }
    }
  }
}
</style>
